<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            margin-top: 20px;
            width: 800px;
            border: 1px solid #ddd;
            border-collapse: collapse;
        }

        td,
        th {
            border: 1px solid #ddd;
            padding: 5px 10px;
        }

        td {
            padding: 0;
            height: 30px;
        }

        td input {
            width: 100%;
            height: 30px;
            border: none;
            background-color: #ffc;
        }
    </style>
    <script>
        window.onload = function(){
            var username = document.getElementById('username');
        var age = document.getElementById('age');
        var tel = document.getElementById('tel');
        var qq = document.getElementById('qq');
        var btnSave = document.getElementById('btnSave');
        var tableList = document.getElementById('tableList');
        var tbody = tableList.children[1];
    
        var index = 1;
    
        btnSave.onclick = function(){
            var tr = document.createElement('tr');
            tr.innerHTML = '<td>' + index++ + '</td><td class="name">' + username.value + '</td><td class="name">' + age.value + '</td><td class="name">' + tel.value + '</td><td class="name">' + qq.value + '</td><td><button class="btnDel">删除</td>';
            tbody.appendChild(tr);
            var btnDel = document.getElementsByClassName('btnDel');
            for(var i=0;i<btnDel.length;i++){
                btnDel[i].onclick = function(){
                    tbody.removeChild(this.parentNode.parentNode);
                }
            }
            
            var flag = false;
            var $name = document.querySelectorAll('.name');
    
            $name.forEach(function(item,index){
                item.onclick = function(){
                    if(!flag){
                        var input = document.createElement('input');
                        input.value = item.innerHTML;
                        item.innerHTML = ''
                        item.appendChild(input);
                        input.focus();
                        flag = true
                    }
                    input.onblur = function(){
                        flag = false
                    item.innerHTML = input.value
                    }
                }
    
            })    
        }
        }
    </script>
</head>
<body>
    <label for="username">姓名</label>
    <input type="text" id="username" name="username">
    <label for="age">年龄</label>
    <input type="text" id="age" name="age">
    <label for="tel">电话</label>
    <input type="text" id="tel" name="tel">
    <label for="qq">QQ号</label>
    <input type="text" id="qq" name="qq">
    <button id="btnSave">保存</button>
    <div class="list">
        <table id="tableList">
            <thead>
                <tr>
                    <th width="40">序号</th>
                    <th width="160">姓名</th>
                    <th width="60">年龄</th>
                    <th width="160">电话</th>
                    <th width="160">QQ号</th>
                    <th width="60">操作</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
</body>
</html>